package top.anka1.fxgui;

import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.Stage;
import top.anka1.ims.ZooManagementSystem.*;
/**
 * RemoveAnimalController is a controller class for the Remove Animal view in the Zoo Management System.
 * It handles the logic for removing an animal from a specified zoo.
 * @author Anka
 */
public class RemoveAnimalController {
    Stage stage;
    Zoo targetZoo;
    @FXML
    private Label removeAnimalTitle;
    @FXML
    private TextField removeAnimalName;

    public void setData(Stage stage, Zoo targetZoo) {
        this.stage = stage;
        this.targetZoo = targetZoo;
        removeAnimalTitle.setText("Remove Animal from " + targetZoo.getName());
    }
    private void showError(Stage stage, String title, String HeaderText, String contentText) {
        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.initOwner(stage);
        alert.setTitle(title);
        alert.setHeaderText(HeaderText);
        alert.setContentText(contentText);
        alert.showAndWait();
    }
    @FXML
    public void confirmRemoveAnimal() {
        String name = removeAnimalName.getText();
        if (name.isEmpty()) {
            showError(this.stage, "Input Error", "Missing Information", "Please fill in all fields.");
            return;
        }
        try {
            if (targetZoo.findAnimal(name) != -1) {
                targetZoo.deleteAnimal(name);
                Alert successAlert = new Alert(Alert.AlertType.INFORMATION);
                successAlert.initOwner(this.stage);
                successAlert.setTitle("Success");
                successAlert.setHeaderText(null);
                successAlert.setContentText("Animal removed successfully!");
                successAlert.showAndWait();
            } else {
                showError(this.stage, "Animal Not Found", "No such animal in the zoo", "Please check the animal name.");
            }
        } catch (Exception e) {
            showError(this.stage, "Error", "An error occurred", "Error: " + e.getMessage());
        }
    }
}
